/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xq.gwt.ui.test;

import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;
import java.util.HashMap;

import xq.gwt.ui.widgets.feedback.FeedbackPresenter;

/**
 *
 * @author db2admin
 */
public class Navigator extends TreeGrid{
    
    public static class ActionTreeNode extends TreeNode {

         public ActionTreeNode(String actionId, String parentFolder, String name) {
             setEmployeeId(actionId);
             setReportsTo(parentFolder);
             setName(name);             
         }

         public void setEmployeeId(String value) {
             setAttribute("actionId", value);
         }

         public void setReportsTo(String value) {
             setAttribute("parentFolder", value);
         }

        @Override
         public void setName(String name) {
             setAttribute("Name", name);
         }

     }

    protected HashMap<String, UserAction> actionMap = new HashMap<String, UserAction>();
    protected FeedbackPresenter feedbackPresenter;
    protected ActionTreeNode[] actionNodes;

    public Navigator(UserAction[] actions, FeedbackPresenter feedbackPresenter){
        this.feedbackPresenter = feedbackPresenter;        
        actionNodes = new ActionTreeNode[actions.length + 1];
        ActionTreeNode root = new ActionTreeNode("root", "1", "Таблици");
        actionNodes[0] = root;
        for (int i = 0; i < actions.length; i++) {            
            actionNodes[i+1] = new ActionTreeNode(actions[i].getName(), "root", actions[i].getCaption());
            actionMap.put(actions[i].getName(), actions[i]);
        }

        initializeTree();
    }

     protected void initializeTree() {
        TreeGridField treeField = new TreeGridField("Name", "Navigator");
        treeField.setCanSort(false);
        setFields(treeField);
        final Tree tree = new Tree();
        tree.setModelType(TreeModelType.PARENT);
        tree.setNameProperty("Name");
        tree.setTitleProperty("Title");
        tree.setIdField("actionId");
        tree.setParentIdField("parentFolder");
        tree.setShowRoot(true);        
        tree.setData(actionNodes);
        setData(tree);        

        addDrawHandler(new DrawHandler() {
            public void onDraw(DrawEvent event) {
                tree.openAll();
            }

        });


            addCellClickHandler(new CellClickHandler() {

            public void onCellClick(CellClickEvent event) {
                String actionId = event.getRecord().getAttribute("actionId");
                UserAction eventAction = actionMap.get(actionId);
                try {
                    if (eventAction != null){
                        eventAction.execute();
                    }
                    
                } catch (Exception ex) {
                    feedbackPresenter.showFeedback(ex.getMessage());
                }
            }
        });        

    }

}
